#include "sdk.h"

#ifndef CB_PRECOMP
#include <wx/intl.h>
#include <wx/string.h>
#include "globals.h"
#include "manager.h"
#include "configmanager.h"
#endif

#ifdef TRACE_SYMTAB
#ifndef CB_PRECOMP
#include "logmanager.h"
#endif
#endif

#include <wx/choicdlg.h>
#include <wx/filedlg.h>
#include "symtab.h"
#include "symtabconfig.h"
#include "symtabexec.h"

namespace {
  PluginRegistrant<SymTab> reg( _T( "SymbolTable" ) );
};

SymTab::SymTab() : CfgDlg( 0L ), ExeDlg( 0L ) {
  if( !Manager::LoadResource( _T( "SymbolTable.zip" ) ) ) {
    NotifyMissingFile( _T( "SymbolTable.zip" ) );
  }
}


SymTab::~SymTab() {
}


void SymTab::OnAttach() {
}


void SymTab::OnRelease( bool ) {
  if( CfgDlg ) {
    CfgDlg->Destroy();
    CfgDlg = 0L;
  }
  if( ExeDlg ) {
    ExeDlg->Destroy();
    ExeDlg = 0L;
  }
}


int SymTab::Execute() {
  if( !IsAttached() )
  { return -1; }
  #ifdef TRACE_SYMTAB
  Manager::Get()->GetLogManager()->DebugLog( F( _T( "SymTab::Execute" ) ) );
  #endif
  if( !CfgDlg )
  { CfgDlg = new SymTabConfigDlg( Manager::Get()->GetAppWindow() ); }
  if( ( !CfgDlg ) || ( CfgDlg->Execute() != wxID_OK ) )
  { return -1; }
  ConfigManager* cfg = Manager::Get()->GetConfigManager( _T( "symtab" ) );
  struct_config config;
  config.choWhatToDo    = cfg->ReadInt( _T( "/what_to_do" ),   0 );
  config.txtLibraryPath = cfg->Read( _T( "/library_path" ), wxEmptyString );
  config.chkIncludeA    = cfg->ReadBool( _T( "/include_a" ),    true );
  config.chkIncludeLib  = cfg->ReadBool( _T( "/include_lib" ),  true );
  config.chkIncludeO    = cfg->ReadBool( _T( "/include_o" ),    false );
  config.chkIncludeObj  = cfg->ReadBool( _T( "/include_obj" ),  false );
  config.chkIncludeDll  = cfg->ReadBool( _T( "/include_dll" ),  false );
  config.txtLibrary     = cfg->Read( _T( "/library" ),      wxEmptyString );
  config.txtSymbol      = cfg->Read( _T( "/symbol" ),       wxEmptyString );
  config.txtNM          = cfg->Read( _T( "/nm" ),           wxEmptyString );
  config.chkDebug       = cfg->ReadBool( _T( "/debug" ),        false );
  config.chkDefined     = cfg->ReadBool( _T( "/defined" ),      false );
  config.chkDemangle    = cfg->ReadBool( _T( "/demangle" ),     false );
  config.chkExtern      = cfg->ReadBool( _T( "/extern" ),       false );
  config.chkSpecial     = cfg->ReadBool( _T( "/special" ),      false );
  config.chkSynthetic   = cfg->ReadBool( _T( "/synthetic" ),    false );
  config.chkUndefined   = cfg->ReadBool( _T( "/undefined" ),    false );
  if( !ExeDlg )
  { ExeDlg = new SymTabExecDlg( Manager::Get()->GetAppWindow() ); }
  if( ( !ExeDlg ) || ( ExeDlg->Execute( config ) != wxID_OK ) )
  { return -1; }
  return 0;
}
